package com.jiukuaidao.zhulin.module.address.presenter;

import com.jiukuaidao.library.net.MyRetroCallBack;
import com.jiukuaidao.zhulin.api.ApiGetAddress;
import com.jiukuaidao.zhulin.entity.ApiHistoryAddressInfo;
import com.jiukuaidao.zhulin.module.BasePresenter;
import com.jiukuaidao.zhulin.module.address.contract.AddressManagerContract;

import java.util.ArrayList;
import java.util.List;


/**
 * Author : PaiTaSuo
 * Date : 2016/5/30
 * Description :
 */
public class AddressMangerPresenter extends BasePresenter<AddressManagerContract.View, AddressManagerContract.Model> implements AddressManagerContract.Presenter {
    public AddressMangerPresenter(AddressManagerContract.Model repository, AddressManagerContract.View view) {
        super(repository, view);
    }

    @Override
    public void loadData() {

        mRepository.loadData(new ApiGetAddress<>(mView.index(), new MyRetroCallBack<ApiHistoryAddressInfo>() {
            @Override
            public void onSuccess(ApiHistoryAddressInfo result, String errorCode, String msg) {

                if (result != null) {
                    List<ApiHistoryAddressInfo.AddressInfo> addressList = result.shoppingAddressList;
                    if (mRepository.getAddressList() == null) {
                        mRepository.setAddressList(new ArrayList<ApiHistoryAddressInfo.AddressInfo>());
                    }
                    if (mView.refreshOrLoad() == AddressManagerContract.REFRESH) {
                        mRepository.getAddressList().clear();
                    }
                    if (addressList != null) {
                        mRepository.getAddressList().addAll(addressList);
                    }
                    if (mRepository.getAddressList() != null && !mRepository.getAddressList().isEmpty()) {
                        mView.getAddressInfo(mRepository.getAddressList());
                    } else {
                        // 显示空页面
                        mView.toast("收货地址为空");
                    }
                }


            }

            @Override
            public void onError(Throwable t, int resCode, String msg) {
                mView.toast(resCode + "");
            }

            @Override
            public void onFail(ApiHistoryAddressInfo result, String errorCode, String msg) {
                mView.toast(msg);
            }
        }));

    }


    @Override
    public void start() {
        loadData();
    }
}
